﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace ELS.User
{    
    public class GroupController
    {
        GroupDataAccess groupDataAccess = new GroupDataAccess();
        internal DataTable GetAllGroups()
        {
            return groupDataAccess.SelectAllGroups();
        }
        internal Dictionary<String, int> GetAllGroupID()
        {
            Dictionary<String, int> Groups = new Dictionary<String, int>();
            DataTable dtAllGroups = new DataTable();
            dtAllGroups = groupDataAccess.SelectAllGroups();
            for (int i = 0; i < dtAllGroups.Rows.Count; i++)
            {
                Groups.Add(dtAllGroups.Rows[i]["GROUPNAME"].ToString(), int.Parse(dtAllGroups.Rows[i]["GROUP_ID"].ToString()));
            }
            return Groups;
        }
        internal Dictionary<String, int> SelectGroupIDByUsername(string selectUsername)
        {
            Dictionary<String, int> userGroups = new Dictionary<String, int>();
            try
            {
                DataTable dtGroups = new DataTable();
                dtGroups = groupDataAccess.SelectGroupsByUsername(selectUsername);
                if (dtGroups.Rows.Count > 0)
                {
                    for (int i = 0; i < dtGroups.Rows.Count; i++)
                    {
                        userGroups.Add(dtGroups.Rows[i]["GROUPNAME"].ToString(), int.Parse(dtGroups.Rows[i]["GROUP_ID"].ToString()));
                    }
                }
            }
            catch (Exception e)
            {
                return userGroups;
            }
            return userGroups;
        }

        internal bool LoadCheckListBox(CheckedListBox chkbGroups)
        {
            try
            {

                ((ListBox)chkbGroups).DataSource = groupDataAccess.SelectAllGroups();
                ((ListBox)chkbGroups).DisplayMember = "GROUPNAME";
                ((ListBox)chkbGroups).ValueMember = "GROUP_ID";
                ((ListBox)chkbGroups).Tag = "GROUP_ID";

                // Clear first hightlight item in list
                for (int i =0; i< chkbGroups.Items.Count; i++){
                    chkbGroups.SetSelected(i, false);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        internal DataTable SelectGroupById(int groupId)
        {
            return groupDataAccess.SelectGroupById(groupId);
        }

        internal DataTable SelectGroupByName(string groupName)
        {
            return groupDataAccess.SelectGroupByName(groupName);
        }

        internal bool UpdateGroup(int currentGrpId, string grpName, string grpDes)
        {
            //int groupid = int.Parse(groupDataAccess.SelectGroupByName(currentGrpId).Rows[0]["GROUP_ID"].ToString());
            int rowUpdate = groupDataAccess.UpdateGroup(currentGrpId, grpName, grpDes);

            if (rowUpdate > 0)
                return true;
            else
                return false;
        }

        internal bool AddNewGroup(string grpName, string grpDes)
        {
            bool isExistGroup = groupDataAccess.SelectGroupByName(grpName).Rows.Count > 0;
            int rowInsert = 0;
            
            if (!isExistGroup)
            {
                rowInsert = groupDataAccess.InsertGroup(grpName, grpDes);
            }
            if (rowInsert > 0)
                return true;
            else
                return false;
        }

        GrantDataAccess grantDataAccess = new GrantDataAccess();
        UserDataAccess userDataAccess = new UserDataAccess();
        internal bool DeleteSelectGroup(List<int> list)
        {
            string listGroup = "";
            for (int i = 0; i < list.Count; i++)
            {
                listGroup += list[i].ToString() + ",";
            }
            listGroup = listGroup.Substring(0, listGroup.Length - 1);

            bool isDeleteUserGrpSuccess = userDataAccess.DeleteUsingUserGrpInListByGroupId(listGroup) >= 0;
            if (isDeleteUserGrpSuccess)
            {
                bool isDeleteGrpPerSuccess = grantDataAccess.DeleteUsingGrpPerInListByGroupId(listGroup) >= 0;
                if (isDeleteGrpPerSuccess)
                {
                    bool isDeleteSuccess = groupDataAccess.DeleteGroup(listGroup) > 0;
                    if (isDeleteSuccess)
                        return true;
                    else
                        return false;
                }
                else return false;
            }
            else return false;            
        }        
    }
}
